package com.stormragetech.teamwork.spring.dao.impl;

import com.stormragetech.common.dao.MongoDao;
import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.teamwork.common.constant.Constants;
import com.stormragetech.teamwork.entity.TwPos;
import com.stormragetech.teamwork.spring.dao.TwPosMongo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Repository
public class TwPosMongoImpl extends MongoDao<TwPos> implements TwPosMongo {

    @Autowired
    public TwPosMongoImpl(MongoTemplate template) {
        super(template);
    }

    @Override
    public void create(final TwPos entity) throws StormragetechException {

        try {

            insert(entity, Constants.TW_POS_COL);
        } catch (Exception e) {
            throw new StormragetechException(Constants.MONGO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public TwPos findById(final String id) throws StormragetechException {

        try {

            Query query = new Query(Criteria.where("pos_id").is(id));
            return findOne(query, Constants.MONGO_ERR_CODE, TwPos.class);
        } catch (Exception e) {
            throw new StormragetechException(Constants.MONGO_ERR_CODE, e.getMessage(), e);
        }
    }
}
